package com.dcbeida.widget;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.dcbeida.R;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created on 2016/7/19.
 * Author:crs
 * Description:XXX
 */
public class PhotoViewShow extends Activity {

    private int position;
    private ArrayList<String> mImages;
    private ArrayList<String> mTitles;
    private ViewPager hViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_show);

        getFrontPageData();
        initViews();

    }

    private void initViews() {
        hViewPager = (ViewPager) findViewById(R.id.hViewPager);
        hViewPager.setAdapter(new ImageAdapter());
        //为ViewPager当前page的数字  
        hViewPager.setCurrentItem(position);
    }

    public void getFrontPageData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {

            //点击图片的位置
            if (bundle.containsKey("position")) {
                position = bundle.getInt("position");
            }
            //获取传递过来的图片地址
            if (bundle.containsKey("mImages")) {
                mImages = bundle.getStringArrayList("mImages");
            }
            //图片对应的标题
            if (bundle.containsKey("mTitles")) {
                mTitles = bundle.getStringArrayList("mTitles");
            }
        }
    }


    public class ImageAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return null == mImages ? 0 : mImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //创建显示图片的控件  
            PhotoView photoView = new PhotoView(container.getContext());
            //设置背景颜色  
            photoView.setBackgroundColor(Color.BLACK);
            //把photoView添加到viewpager中，并设置布局参数  
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            //加载当前PhtotoView要显示的数据  
            String url = mImages.get(position);

            if (!TextUtils.isEmpty(url)) {
                //使用使用Glide进行加载图片进行加载图片
                String imgurl=url.replaceAll("\\\\", "");
                Glide.with(PhotoViewShow.this).load(imgurl).into(photoView);
            }

            //图片单击事件的处理  
            photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    finish();
                }
            });
            return photoView;
        }

    }
} 